﻿package net.chrisjrock.iface
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.utils.Dictionary;
	
	/**
	 * ...
	 * @author Christopher J. Rock
	 */
	public class InterfaceEngineer
	{
		public static function buttonize (button:InteractiveObject , onClick:Function = null , onRollOver:Function = null , onRollOut:Function = null):InteractiveObject {
			button.mouseEnabled = true;
			if (onClick != null) button.addEventListener(MouseEvent.CLICK , onClick);
			if (onRollOver != null) button.addEventListener(MouseEvent.ROLL_OVER , onRollOver);
			if (onRollOut != null) button.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
			if (button is DisplayObjectContainer) DisplayObjectContainer(button).mouseChildren = false;
			if (button is Sprite) Sprite(button).buttonMode = true;
			return button;
		}
		
		public static function unbuttonize (button:InteractiveObject , onClick:Function = null , onRollOver:Function = null , onRollOut:Function = null):InteractiveObject {
			button.mouseEnabled = false;
			if (onClick != null) button.removeEventListener(MouseEvent.CLICK , onClick);
			if (onRollOver != null) button.removeEventListener(MouseEvent.ROLL_OVER , onRollOver);
			if (onRollOut != null) button.removeEventListener(MouseEvent.ROLL_OUT, onRollOut);
			if (button is Sprite) Sprite(button).buttonMode = false;
			return button;
		}
		
		public static function drawOverlay (bounds:Rectangle , overlayContainer:Sprite = null , color:uint = 0x000000 , alpha:Number = 0):Sprite {
			if (!overlayContainer) overlayContainer = new Sprite();
			overlayContainer.graphics.lineStyle(0 , 0 , 0);
			overlayContainer.graphics.beginFill(color , alpha);
			overlayContainer.graphics.moveTo(bounds.left , bounds.top);
			overlayContainer.graphics.lineTo(bounds.right , bounds.top);
			overlayContainer.graphics.lineTo(bounds.right , bounds.bottom);
			overlayContainer.graphics.lineTo(bounds.left , bounds.bottom);
			overlayContainer.graphics.endFill();
			return overlayContainer;
		}
		
		public static function drawDisplayOverlay (display:DisplayObject):Sprite { return drawOverlay(display.getBounds(display)); }
		
		public static function addOverlay(display:Sprite):Sprite {
			display.addChild(drawOverlay(display.getBounds(display)));
			return display;
		}
		
		public static function buttonizeContainer (container:DisplayObjectContainer , onClick:Function = null , onRollOver:Function = null , onRollOut:Function = null):DisplayObjectContainer {
			container.addChild(drawDisplayOverlay(container)).visible = false;
			return DisplayObjectContainer(buttonize(container , onClick , onRollOver , onRollOut));
		}
		
		public static function buttonizeSprite (container:Sprite , onClick:Function = null , onRollOver:Function = null , onRollOut:Function = null):Sprite {
			return Sprite(buttonize(addOverlay(container) , onClick , onRollOver , onRollOut));
		}
		
		public static function wrapButton (content:DisplayObject , onClick:Function = null , onRollOver:Function = null , onRollOut:Function = null):Sprite {
			var wrapper:Sprite = drawDisplayOverlay(content);
			wrapper.x = content.x;
			wrapper.y = content.y;
			content.x = 0;
			content.y = 0;
			wrapper.addChild(content);
			return wrapper;
		}
		
		public static function getURL(url:String , window:String = "_blank"):void { navigateToURL(new URLRequest(url) , window); }
		public static function onTextLink(event:TextEvent):void { getURL(event.text); }
		
		public static const linkIndex:Dictionary = new Dictionary();
		private static function linkHandler (event:Event):void { getURL(linkIndex[event.target]); }
		
		public static function linkize (button:InteractiveObject , url:String , buttonize:Boolean = true , linkEventType:String = "click"):InteractiveObject {
			linkIndex[button] = url;
			button.addEventListener(linkEventType , linkHandler);
			if (buttonize) InterfaceEngineer.buttonize(button);
			return button;
		}
		
		public static function unlinkize (button:InteractiveObject , unbuttonize:Boolean = true , linkEventType:String = "click"):InteractiveObject {
			delete linkIndex[button];
			button.removeEventListener(linkEventType , linkHandler);
			if (unbuttonize) InterfaceEngineer.unbuttonize(button);
			return button;
		}
	}
}